﻿namespace CodingExercises._1_25;

public class CountCharacterFrequencies
{
    // 统计字符串中字符的出现频率
    public static Dictionary<char, int> Mehotd1(string input)
    {
        var result = new Dictionary<char, int>();
        foreach (var item in input)
        {
            if (result.ContainsKey(item))
            {
                result[item]++;
            }
            else
            {
                result[item] = 1;
            }
        }

        return result;
    }

    public static Dictionary<char, int> Mehotd2(string input)
    {
        return input.
            GroupBy(item => item)
            .ToDictionary(group => 
                group.Key, group => group.Count());
    }
}